aiui.define('buildform',['jquery','form'], function($,form){
    var buildform ={
        v:'1.0.1',

    };
    /**
     * 初始化buildform
     */
    buildform.init = function(){
        //初始化form表单
        form.init();
        //切换表单事件
        $('.aiui-tabs-item').on('click',function(e){
            var that = $(this),tabs=that.parents('.aiui-tabs'),panel=that.attr('aiui-controls'),
            url = that.data('url'),pjax = that.data('type');
            active_panel=tabs.find('.aiui-tabs-content').find('.aiui-tabs-panel[aiui-panel-name="'+panel+'"]');
            if(pjax && pjax=='pjax'){
                $.pjax({url: url, container: '#pjax-container'})
            }else{
                that.addClass('is-active').siblings().removeClass('is-active');
                active_panel.show().siblings().hide();
            }
        })
        //没有用dialog方式的表单提交
        $('.aiui-tabs').on('click','#submit',function(e){
            var that = $(this),form = that.parents('form'),
            data = form.serializeArray();
            var postData={};
            $.each(data, function(i, field){
                if(postData[this.name]){
                    if($.isArray(postData[this.name])){
                        postData[this.name].push(this.value);
                    }else{
                        postData[this.name]=[postData[this.name],this.value];
                    }
                }else{
                    postData[this.name]=this.value;
                }
                return postData;
            });
            $.ajax({
                url:form.attr('action'),
                method:form.attr('method'),
                data:postData,
                dataType:'json',
                success:function(res){
                    if(res.code==1){
                        aiui.$message({
                            type:'success',
                            message:res.msg,
                            duration:'3000',
                            showClose:true,
                            closeed:function(){
                                aiui.$dialog.closeAll();
                                $.pjax.reload('#pjax-container')
                            }
                        })
                    }else{
                        aiui.$message({
                            type:'error',
                            message:res.msg,
                            duration:'3000',
                            showClose:true,
                        })
                    }
                },
                error:function(res){
                    aiui.$message({
                        type:'error',
                        message:res.responseJSON.message,
                        duration:'3000',
                        showClose:true,
                    });
                }
            });
        })
        //使用dialog的方式提交表单
        $('.aiui-dialog').find('button[aiui-btn-key=1]').on('click',function(e){
            //确定点击事件
            var btn = $(this),thisTab =btn.parents('.aiui-dialog').find('.aiui-tabs-list').find('.is-active'),
            tabs=thisTab.parents('.aiui-tabs')
            panel = thisTab.attr('aiui-controls'),
            thisForm = tabs.find('.aiui-tabs-panel[aiui-panel-name="'+panel+'"]').find('form'),
            data = thisForm.serializeArray();
            var postData={};
            $.each(data, function(i, field){
                if(postData[this.name]){
                    if($.isArray(postData[this.name])){
                        postData[this.name].push(this.value);
                    }else{
                        postData[this.name]=[postData[this.name],this.value];
                    }
                }else{
                    postData[this.name]=this.value;
                }
                return postData;
            });
            $.ajax({
                url:thisForm.attr('action'),
                method:thisForm.attr('method'),
                data:postData,
                dataType:'json',
                success:function(res){
                    if(res.code==1){
                        aiui.$message({
                            type:'success',
                            message:res.msg,
                            duration:'3000',
                            showClose:true,
                            closeed:function(){
                                aiui.$dialog.closeAll();
                                $.pjax.reload('#pjax-container')
                            }
                        })
                    }else{
                        aiui.$message({
                            type:'error',
                            message:res.msg,
                            duration:'3000',
                            showClose:true,
                        })
                    }
                }
            });
        })
        //联动下拉框
        $('select[aiui-linkage]').on('change',function(e){
            var that = $(this),value = that.val(),
            url = that.attr('ajax-url'),field =  that.attr('name'),
            postData = {field:field,pid:value},$form=that.parents('[aiui-form]'),
            loadingid = aiui.loading($form[0]),next=$form.find('select[name="'+that.attr('aiui-linkage-filed')+'"]');
            loadingid.show();
            $.ajax({
                url:url,
                dataType:'json',
                data:postData,
                type:'post',
                success:function(res){
                    loadingid.close();
                    var option = [];
                    if(res.code==1){
                        $.each(res.data,function(key,title){
                            option.push('<option value="'+key+'">'+title+'</option>');
                        })
                    }
                    next.html(option.join(''));
                    form.render($form[0],'select',that.attr('aiui-linkage-filed'))
                }
            })
        })
    }
    return buildform;
})